/**
 * Unit Manager
 * Manage Unit mecanics
 **/
var UnitManager = function() {
	// Game objects
	this.units = new Array();

	this.init = function() {
		log('*** INIT UNIT MANAGER ***');
	}

	this.update = function() {
		for (var i = 0; i < this.units.length; i++) {
			this.units[i].update();
		};
	}

	this.render = function() {
		// log('*** RENDER UNIT MANAGER ***');
		for (var i = 0; i < this.units.length; i++) {
			this.units[i].render();
		};
	}

	this.create = function(type, pop) {
		var unit = null;
		switch (type) {
			case 'Worker' : 
				unit = new Unit();
				unit.init(pop);
				this.units.push(unit);
			break;
		}
	}

	this.getSelected = function() {
		var units = new Array();
		for (var i = 0; i < this.units.length; i++) {
			if (this.units[i].selected)
				units.push(this.units[i]);
		}
		return units;
	}

	this.getUnitWithPosition = function(p) {
		var idx = 0;
		var unit = null;
		while (idx < this.units.length && unit == null) {
			if (distance(this.units[idx].position,p) == 0) 
				unit = this.units[idx];
			else
				idx++;
		}

		return unit;
	}
};
